package com.zhang.zxxdocker.controller;


import com.zhang.zxxdocker.entity.User;
import com.zhang.zxxdocker.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhangxiaoxiang
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private IUserService iUserService;

    @GetMapping("/users")
    public Object get(HttpServletRequest request) throws UnknownHostException {
        Map<String, Object> result = new HashMap();
        log.info("进入了控制层,访问接口是:{},请求的IP和端口是{}", request.getRequestURI(), request.getRemoteHost() + ":" + request.getRemotePort());
        log.info("本项目所在的服务器地址:{}", InetAddress.getLocalHost().getHostAddress());
        List<User> list;
        try {
            list = iUserService.list();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("数据库异常:{}", e.getMessage());
            result.put("code", "500");
            result.put("msg", "数据库异常:" + e.getMessage());
            return result;
        }
        list.forEach(user -> log.info("查询到的数据:{}", user));

        result.put("code", 200);
        result.put("msg", "查询成功-3月你好");
        result.put("data", list);
        return result;
    }

}
